package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Patient_dao;
import com.clinic.domain.Patient;
import com.clinic.util.Util_imp;

public class Patient_imp implements Patient_dao {

	SessionFactory session_factory = null;

	public Patient_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public String create_patient(Patient p) {
		System.out.println("Enter - create_patient");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int pt_obj_id = 0;
		try {
			tx = session.beginTransaction();
			pt_obj_id = (Integer) session.save(p);
			tx.commit();
			System.out.println("New Patient Created with ID: " + pt_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Patient Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_patient");
		return p.getToken();
	}

	@Override
	public void update_patient(Patient pt) {
		System.out.println("Enter - update_patient");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(pt);
			tx.commit();
			System.out.println("Patient Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Patient Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_patient");
	}

	@Override
	public void delete_patient(int pt_obj_id) {
		System.out.println("Enter - delete_patient");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Patient p = (Patient) session.get(Patient.class, pt_obj_id);
			session.delete(p);
			tx.commit();
			System.out.println("Patient with ID: " + pt_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Patient Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_patient");
	}

	@Override
	public Patient get_patient(int pt_obj_id) {
		System.out.println("Enter - get_patient");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Patient p = null;
		try {
			tx = session.beginTransaction();
			p = (Patient) session.createQuery("from Patient where pt_obj_id=:id")
					.setParameter("id", pt_obj_id).uniqueResult();
			tx.commit();
			if(p != null){
				System.out.println("Returned Object Name: " + p.getFull_name());
			}
		} catch (HibernateException ex) {
			System.out.println("TESTTEST");
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_patient");
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Patient> get_all_patients() {
		System.out.println("Enter - get_all_patients");
		Session session = session_factory.openSession();
		List<Patient> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Patient").list();
			tx.commit();
			System.out.println("Number of Patients returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_patients");
		return l;
	}

	@Override
	public Patient persistOrMerge(Patient p) {
		return (Patient) session_factory.getCurrentSession().merge(p);
	}

	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Patient").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Patient: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}

	@Override
	public Patient signin(String email_address ,String passwrd) {
		System.out.println("Enter - SignIn");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Patient p = null;
		try {
			tx = session.beginTransaction();
			p = (Patient) session.createQuery("from Patient where email_address=:email_address and passwrd=:passwrd")
					.setParameter("email_address", email_address).setParameter("passwrd", passwrd).uniqueResult();
			tx.commit();

		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - SignIn");
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean is_patient_exist(String email_address) {
		System.out.println("Enter - IS Patient Exist");
		boolean returned_value = false;
		Session session = session_factory.openSession();
		Transaction tx = null;
		List<Patient> l = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Patient where email_address=:email_address")
					.setParameter("email_address", email_address).list();
			tx.commit();
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		if(l.size() == 0){
			returned_value = false;
		} else {
			returned_value = true;
		}
		System.out.println("Returned value : " + returned_value);
		System.out.println("Exit - IS Patient Exist");
		return returned_value;
	}
	
	@Override
	public boolean is_token_valid(String email_addredd, String senttoken) {
		System.out.println("Enter - IS Patient Exist");
		boolean returned_value = false;
		Session session = session_factory.openSession();
		Transaction tx = null;
		Patient p = null;
		try {
			tx = session.beginTransaction();
			p = (Patient) session.createQuery("from Patient where email_address =:email_address")
					.setParameter("email_address", email_addredd).uniqueResult();
			tx.commit();
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		if(p != null && p.isValid_email() == false) {
			if(senttoken.equals(p.getToken())){
				returned_value = true;
				p.setValid_email(true);
				p.setToken("");
				p.setProfile_status("Ok");
			} else {
				returned_value = false;
				p.setValid_email(false);
				p.setProfile_status("Lock");
			}
			update_patient(p);
		}
		System.out.println("Returned value : " + returned_value);
		System.out.println("Exit - IS Patient Exist");
		return returned_value;
	}

	@Override
	public String get_patient(String pt_email_address) {
		System.out.println("Enter - get_patient full_name");
		Session session = session_factory.openSession();
		Transaction tx = null;
		String full_name = "";
		try {
			tx = session.beginTransaction();
			full_name = session.createQuery("select full_name from Patient where email_address = :email")
					.setParameter("email", pt_email_address).uniqueResult().toString();
			tx.commit();
		} catch (HibernateException ex) {
			System.out.println("TESTTEST");
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_patient full_name");
		return full_name;
	}
}